<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备ID" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障时间" prop="faultTime">
        <el-date-picker clearable
          v-model="queryParams.faultTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择故障时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="故障类型" prop="faultType">
        <el-select v-model="queryParams.faultType" placeholder="请选择故障类型" clearable>
          <el-option
            v-for="dict in farm_device_fault_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="严重程度" prop="severity">
        <el-select v-model="queryParams.severity" placeholder="请选择严重程度" clearable>
          <el-option
            v-for="dict in farm_device_severity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="处理状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择处理状态" clearable>
          <el-option
            v-for="dict in farm_device_fault_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['fault_record:fault_record:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['fault_record:fault_record:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['fault_record:fault_record:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['fault_record:fault_record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="fault_recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" type="index" align="center" prop="faultId" />
      <el-table-column label="故障设备名称" align="center" width="200px" >
        <template #default="scope">
          {{scope.row.location}}·{{scope.row.model}}
        </template>
      </el-table-column>
      <el-table-column label="故障时间" align="center" prop="faultTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.faultTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障类型" align="center" prop="faultType">
        <template #default="scope">
          <dict-tag :options="farm_device_fault_type" :value="scope.row.faultType"/>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="description" />
      <el-table-column label="严重程度" align="center" prop="severity">
        <template #default="scope">
          <dict-tag :options="farm_device_severity" :value="scope.row.severity"/>
        </template>
      </el-table-column>
      <el-table-column label="处理状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="farm_device_fault_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['fault_record:fault_record:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['fault_record:fault_record:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改故障记录对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="fault_recordRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="故障设备" prop="equipmentId">
          <el-select v-model="form.equipmentId" placeholder="请选择设备">
            <el-option v-for="item in deviceList" :key="item.equipmentId" :label="`${item.location}·${item.model}`" :value="item.equipmentId"/>
          </el-select>
        </el-form-item>
        <el-form-item label="故障时间" prop="faultTime">
          <el-date-picker clearable
            v-model="form.faultTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择故障时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="故障类型" prop="faultType">
          <el-select v-model="form.faultType" placeholder="请选择故障类型">
            <el-option
              v-for="dict in farm_device_fault_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="严重程度" prop="severity">
          <el-select v-model="form.severity" placeholder="请选择严重程度">
            <el-option
              v-for="dict in farm_device_severity"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择处理状态">
            <el-option
              v-for="dict in farm_device_fault_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Fault_record">
import { listFault_record, getFault_record, delFault_record, addFault_record, updateFault_record } from "@/api/fault_record/fault_record";
import {listDevice_manage} from "@/api/device_manage/device_manage.js";

const { proxy } = getCurrentInstance();
const { farm_device_fault_status, farm_device_severity, farm_device_fault_type } = proxy.useDict('farm_device_fault_status', 'farm_device_severity', 'farm_device_fault_type');

const fault_recordList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const deviceList = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    equipmentId: null,
    faultTime: null,
    faultType: null,
    severity: null,
    status: null,
  },
  rules: {
    equipmentId: [
      { required: true, message: "设备ID不能为空", trigger: "blur" }
    ],
    faultTime: [
      { required: true, message: "故障时间不能为空", trigger: "blur" }
    ],
    faultType: [
      { required: true, message: "故障类型不能为空", trigger: "change" }
    ],
    description: [
      { required: true, message: "描述不能为空", trigger: "blur" }
    ],
    severity: [
      { required: true, message: "严重程度不能为空", trigger: "change" }
    ],
    status: [
      { required: true, message: "处理状态不能为空", trigger: "change" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询故障记录列表 */
function getList() {
  loading.value = true;
  listFault_record(queryParams.value).then(response => {
    fault_recordList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 新增和修改时查询设备列表
function getDeviceList() {
  listDevice_manage().then((response) => {
    deviceList.value = response.rows;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    faultId: null,
    equipmentId: null,
    faultTime: null,
    faultType: null,
    description: null,
    severity: null,
    status: null,
    createTime: null,
    updateTime: null,
    updateBy: null,
    remark: null,
    createBy: null
  };
  proxy.resetForm("fault_recordRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.faultId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加故障记录";
  getDeviceList();
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _faultId = row.faultId || ids.value
  getFault_record(_faultId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改故障记录";
  });
  getDeviceList();
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["fault_recordRef"].validate(valid => {
    if (valid) {
      if (form.value.faultId != null) {
        updateFault_record(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFault_record(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _faultIds = row.faultId || ids.value;
  proxy.$modal.confirm('是否确认删除故障记录编号为"' + _faultIds + '"的数据项？').then(function() {
    return delFault_record(_faultIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('fault_record/fault_record/export', {
    ...queryParams.value
  }, `fault_record_${new Date().getTime()}.xlsx`)
}

getList();
</script>

<style scoped>
.el-select{
  width: 150px;
}
</style>